﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LongestFlags.Question;

namespace LongestFlags.Answer
{
    class Bob : ILongestFlag
    {

        public int GetLongestFlags(int[] iArray, out int startIndex, out int endIndex)
        {
            int max = 0;
            startIndex = 0;
            endIndex = 0;

            for (int i = 0; i < iArray.Length; i++)
            {
                for (int j = i; j < iArray.Length; j++)
                {
                    if (IsValid(iArray, i, j))
                    {
                        int cur = j - i + 1;
                        if (cur > max)
                        {
                            startIndex = i;
                            endIndex = j;
                            max = cur;
                        }
                    }
                }
            }

            return max;

        }

        private bool IsValid(int[] iArray, int i, int j)
        {
            int res = 0;
            for (int start = i; start <= j; start++)
            {
                if (iArray[start] > 0)
                {
                    res++;
                }
                else if (iArray[start] < 0)
                {
                    res--;
                }
            }
            return res == 0;
        }


    }
}
